package com.gopay.bis.dao.payconfig.impl;

import com.gopay.bis.dao.payconfig.BisBankExtConfigDAO;
import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.domain.bis.BaseBisBankConfig;
import com.gopay.common.domain.bis.BaseBisBankExtConfig;
import com.gopay.common.domain.bis.BisBcdlIntrConfig;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author liujy
 *
 */
@Repository("bisBankExtConfigDAO")
public class BisBankExtConfigDAOImpl  extends
	BaseDAOImpl<BaseBisBankConfig, String> implements BisBankExtConfigDAO{
	
	public BaseBisBankExtConfig findExtConfigByBankCodeAndBankBusProd(
			BisBankBusinessProd prod, String bank){
		StringBuilder s = new StringBuilder("from ");
		if(BisBankBusinessProd._00.value.equals(prod.value)){
			s.append(" BisEbankB2CConfig ");
		}else if(BisBankBusinessProd._01.value.equals(prod.value)){
			s.append(" BisEbankB2BConfig ");
		}else if(BisBankBusinessProd._02.value.equals(prod.value)){
			s.append(" BisContractPayConfig ");
		}else if(BisBankBusinessProd._05.value.equals(prod.value)) {
			s.append(" BisCreditExPayConfig ");
			//weiyajun 20150527 wap网银支付
		}else if(BisBankBusinessProd._11.value.equals(prod.value)){
			s.append(" BisEbankB2CWAPConfig ");
		}else if(BisBankBusinessProd._15.value.equals(prod.value)){
			s.append(" BisDebitExPayConfig ");
		}else if(BisBankBusinessProd._16.value.equals(prod.value)){
			s.append(" BisScanPayConfig ");
		}else if(BisBankBusinessProd._17.value.equals(prod.value)){
			s.append(" BisAppPayConfig ");
		}else if(BisBankBusinessProd._18.value.equals(prod.value)){
			s.append(" BisOfflineScanPayConfig ");
		}else if(BisBankBusinessProd._20.value.equals(prod.value)){
		    s.append(" BisAdPayConfig ");
		}else if(BisBankBusinessProd._21.value.equals(prod.value)){
            s.append("BisDirectAdPayConfig ");
        }else if(BisBankBusinessProd._25.value.equals(prod.value)){
		    s.append("BisMicroPayConfig ");
		}
		s.append(" c where c.bankCode=:bankCode");
		s.append(" and c.del=:del");
		s.append(" and c.enable=:enable");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("bankCode", bank);
		param.put("del", false);
		param.put("enable", true);
		
		List<BaseBisBankConfig> list = findByHql(s.toString(), param);
		if(null == list || list.isEmpty()){
			return null;
		}
		return (BaseBisBankExtConfig)list.get(0);
	}
	
	public BisBcdlIntrConfig findBcdlIntrConfigByBankCode(String bankCode){
		StringBuilder s = new StringBuilder("from ");
		s.append(" BisBcdlIntrConfig ");
		s.append(" c where c.bankCode=:bankCode");
		s.append(" and c.del=:del");
		s.append(" and c.enable=:enable");
		
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("bankCode", bankCode);
		param.put("del", false);
		param.put("enable", true);
		
		List<BaseBisBankConfig> list = findByHql(s.toString(), param);
		if(null == list || list.isEmpty()){
			return null;
		}
		return (BisBcdlIntrConfig)list.get(0);
	}

	@Override
	public List<BaseBisBankConfig> findAllBcdlIntrConfig() {
		StringBuilder s = new StringBuilder("from ");
		s.append(" BisBcdlIntrConfig ");
		s.append(" c where 1 = 1");
		s.append(" and c.del=:del");
		s.append(" and c.enable=:enable");

		Map<String, Object> param = new HashMap<String, Object>();
		param.put("del", false);
		param.put("enable", true);
		
		List<BaseBisBankConfig> list = findByHql(s.toString(), param);
		return list;
	}

	@Override
	public List<BaseBisBankConfig> findAllEbankCustConfig() {
		StringBuilder s = new StringBuilder("from ");
		s.append(" BisEbankCustConfig ");
		s.append(" c where 1 = 1");
		s.append(" and c.del=:del");
		s.append(" and c.enable=:enable");
		
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("del", false);
		param.put("enable", true);
		List<BaseBisBankConfig> list = findByHql(s.toString(), param);
		return list;
	}

	@Override
	public BaseBisBankExtConfig findExtConfigByBankChannelAndBankBusProd(
			BisBankBusinessProd prod, BisBankInterfaceChannel channel,
			String bank) {
		StringBuilder s = new StringBuilder("from ");
		if(BisBankBusinessProd._00.value.equals(prod.value)){
			s.append(" BisEbankB2CConfig ");
		}else if(BisBankBusinessProd._01.value.equals(prod.value)){
			s.append(" BisEbankB2BConfig ");
		}else if(BisBankBusinessProd._02.value.equals(prod.value)){
			s.append(" BisContractPayConfig ");
		}else if(BisBankBusinessProd._05.value.equals(prod.value)){
			s.append(" BisCreditExPayConfig ");
			//weiyajun 20150624 wap网银支付
		}else if(BisBankBusinessProd._11.value.equals(prod.value)){
			s.append(" BisEbankB2CWAPConfig ");
		}else if(BisBankBusinessProd._15.value.equals(prod.value)){
			s.append(" BisDebitExPayConfig ");
		}else if(BisBankBusinessProd._16.value.equals(prod.value)){
			s.append(" BisScanPayConfig ");
		}else if(BisBankBusinessProd._17.value.equals(prod.value)){
			s.append(" BisAppPayConfig ");
		}else if(BisBankBusinessProd._18.value.equals(prod.value)){
			s.append(" BisOfflineScanPayConfig ");
		}else if(BisBankBusinessProd._20.value.equals(prod.value)){
		    s.append(" BisAdPayConfig ");
		}else if(BisBankBusinessProd._21.value.equals(prod.value)){
            s.append(" BisDirectAdPayConfig ");
        }else if(BisBankBusinessProd._25.value.equals(prod.value)){
		    s.append(" BisMicroPayConfig ");
		}
		s.append(" c where c.bankCode=:bankCode");
		s.append(" and c.chCode=:chCode");
		s.append(" and c.del=:del");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("bankCode", bank);
		param.put("chCode", channel.value);
		param.put("del", false);
		
		List<BaseBisBankConfig> list = findByHql(s.toString(), param);
		if(null == list || list.isEmpty()){
			return null;
		}
		return (BaseBisBankExtConfig)list.get(0);
	}

    @Override
    public BaseBisBankExtConfig findfindExtConfigByBankBusProdAndChannel(BisBankBusinessProd prod,
            BisBankInterfaceChannel channel) {
        StringBuilder s = new StringBuilder("from ");
        if(BisBankBusinessProd._00.value.equals(prod.value)){
            s.append(" BisEbankB2CConfig ");
        }else if(BisBankBusinessProd._01.value.equals(prod.value)){
            s.append(" BisEbankB2BConfig ");
        }else if(BisBankBusinessProd._02.value.equals(prod.value)){
            s.append(" BisContractPayConfig ");
        }else if(BisBankBusinessProd._05.value.equals(prod.value)){
            s.append(" BisCreditExPayConfig ");
            //weiyajun 20150624 wap网银支付
        }else if(BisBankBusinessProd._11.value.equals(prod.value)){
            s.append(" BisEbankB2CWAPConfig ");
        }else if(BisBankBusinessProd._15.value.equals(prod.value)){
            s.append(" BisDebitExPayConfig ");
        }else if(BisBankBusinessProd._16.value.equals(prod.value)){
            s.append(" BisScanPayConfig ");
        }else if(BisBankBusinessProd._17.value.equals(prod.value)){
            s.append(" BisAppPayConfig ");
        }else if(BisBankBusinessProd._18.value.equals(prod.value)){
            s.append(" BisOfflineScanPayConfig ");
        }else if(BisBankBusinessProd._20.value.equals(prod.value)){
            s.append(" BisAdPayConfig ");
        }else if(BisBankBusinessProd._25.value.equals(prod.value)){
            s.append(" BisMicroPayConfig ");
        }
        s.append(" c where 1=1 ");
        s.append(" and c.chCode=:chCode");
        s.append(" and c.del=:del");
        Map<String, Object> param = new HashMap<String, Object>();
       // param.put("bankCode", bank);
        param.put("chCode", channel.value);
        param.put("del", false);
        
        List<BaseBisBankConfig> list = findByHql(s.toString(), param);
        if(null == list || list.isEmpty()){
            return null;
        }
        return (BaseBisBankExtConfig)list.get(0);
    }
	

	@Override
	public BaseBisBankExtConfig findExtConfigByBankChannel(BisBankInterfaceChannel channel) {
		StringBuilder s = new StringBuilder("from ");
		s.append(" BisDebitExPayConfig ");
		s.append(" c where c.chCode=:chCode");
		s.append(" and c.del=:del");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("chCode", channel.value);
		param.put("del", false);
		
		List<BaseBisBankConfig> list = findByHql(s.toString(), param);
		if(null == list || list.isEmpty()){
			return null;
		}
		return (BaseBisBankExtConfig)list.get(0);
	}
	
}